#include "rk2App.h"
#include <math.h>

using namespace std;
namespace rkwolek {

    Rk2App::Rk2App(double t0, double x0, double T, double(*f)(double, double)) {
        this->tini = t0;
        this->ison = x0;
        this->tend = T;
        this->sfn = f;
    }

    double* Rk2App::function(int n) {
        /*
        double *x = new double [n + 1];
        double h = (tend - tini) / n;
        x[0] = ison;
        for (int k = 0; k < n; k++) {
            double tp = tini + k*h;
            double f = h * sfn(tp, x[k]);
            x[k + 1] = x[k] + 0.5 * (f + h * sfn(tp + h, x[k] + f));
        }
        return x;
         */
        double *x = new double [n + 1];
        double h = 1.0 / 100.0; //(tend-tini)/n;
        x[0] = ison;

        for (int k = 0; k < n; k++) {
            //double F1 = (h * k) * sfn(tini + h*k, x[k]);
            //double F2 = (h * k) * sfn(tini + h * k + 1, x[k] + F1 / 2.0);
            //x[k + 1] = x[k] + h/2.0*F2;
            double tp = tini + k*h;
            double f = h * sfn(tp, x[k]);
            x[k + 1] = x[k] + 0.5 * (f + h * sfn(tp + h, x[k] + f));
        }
        return x;
    }
}
